﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IWMAG.Core.Objects;
using Microsoft.Xna.Framework;

namespace IWMAG.Core.Hitboxes {
    public struct RectHitbox : IHitbox {
        public Rectangle Inner;
        public Vector2 Origin { get; set; }

        public RectHitbox(Rectangle rect) : this() {
            Inner = rect;
        }

        public void Shift(Vector2 rel) {
            Inner.X += (int)rel.X;
            Inner.Y += (int)rel.Y;
        }

        public IEnumerable<Vector2> GetPoints() {
            return new Vector2[] { 
                new Vector2(Inner.Left, Inner.Top),
                new Vector2(Inner.Right, Inner.Top),
                new Vector2(Inner.Left, Inner.Bottom),
                new Vector2(Inner.Right, Inner.Bottom),
                new Vector2(Inner.Center.X, Inner.Center.Y)
            };
        }

        public bool Contains(Point a) {
            return Inner.Contains(a);
        }

        public bool Contains(Vector2 a) {
            return Inner.Contains((int)a.X, (int)a.Y);
        }

        public bool CollidesWith(IHitbox a) {
            foreach (Vector2 p in a.GetPoints()) {
                if (Inner.Contains(new Point((int)p.X, (int)p.Y))) {
                    return true;
                }
            }

            return false;
        }

        public bool CollidesWith(RectHitbox a) {
            return Inner.Intersects(a.Inner);
        }

        public void Update(Vector2 pos) {
            pos -= Origin / 2;
            Inner.Location = new Point((int)pos.X, (int)pos.Y);
        }

        public IHitbox Add(Vector2 shift) {
            return new RectHitbox(new Rectangle(Inner.Left + (int)shift.X, Inner.Top + (int)shift.Y, Inner.Width, Inner.Height));
        }
    }
}
